package com.ae.controller;

import com.ae.pojo.User;
import com.ae.service.UserService;
import com.ae.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author bug-termination
 * @date 2020-10-20 08:16
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("loginUser")
    @ResponseBody
    public String loginUser(String email, String password, HttpSession httpSession) {
        User user = userService.loginUser(email, password);
        httpSession.setAttribute("userAccount", user.getEmail());
        return "success";
    }

    @RequestMapping("loginOut")
    public String loginOut(HttpSession httpSession) {
        httpSession.invalidate();
        return "redirect:/subject/selectAll";
    }

    @RequestMapping("loginOut2")
    public String loginOut2(HttpSession httpSession) {
        httpSession.invalidate();
        return "redirect:/subject/selectAll";
    }

    @RequestMapping("insertUser")
    @ResponseBody
    public String insertUser(User user) {
        int flag = userService.insertSelective(user);
        if (flag != 0) {
            return "success";
        } else {
            return "false";
        }
    }

    @RequestMapping("validatePassword")
    @ResponseBody
    public String validatePassword(String password, HttpSession httpSession) {
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        if (user.getPassword().equals(password)) {
            return  "success";
        } else {
            return  "false";
        }
    }

    @RequestMapping("validateEmail")
    @ResponseBody
    public String validateEmail(String email) {
        List<User> userList = userService.selectAll();
        String flag = "success";
        for (User user : userList) {
            if (user.getEmail().equals(email)) {
                flag = "false";
            }
        }
        return flag;
    }

    @RequestMapping("validateEmailCode")
    public ModelAndView validateEmailCode(@RequestParam("code") String code, @RequestParam("email") String email) {
        User user = userService.selectByEmail(email);
        boolean flag = user.getCode().equals(code);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("email", email);
        if (flag) {
            modelAndView.setViewName("/before/reset_password.jsp");
            return modelAndView;
        } else {
            modelAndView.setViewName("/before/forget_password.jsp");
            return modelAndView;
        }
    }

    @RequestMapping("showMyProfile")
    public ModelAndView showMyProfile(HttpSession httpSession) {
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        modelAndView.setViewName("/before/my_profile.jsp");
        return modelAndView;
    }

    @RequestMapping("changeProfile")
    public ModelAndView changeProfile(HttpSession httpSession) {
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        modelAndView.setViewName("/before/change_profile.jsp");
        return modelAndView;
    }

    @RequestMapping("updateUser")
    public String updateUser(User user) {
        userService.updateByPrimaryKeySelective(user);
        return "redirect:/user/showMyProfile";
    }

    @RequestMapping("changeAvatar")
    public ModelAndView changeAvatar(HttpSession httpSession) {
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        modelAndView.setViewName("/before/change_avatar.jsp");
        return modelAndView;
    }

    @RequestMapping("upLoadImage")
    public String upLoadImage(MultipartFile image_file, HttpSession httpSession) {
        //上传地址
        String path = "F:\\WEBSoft\\apache-tomcat-8.5.41\\webapps\\video\\";
        //上传的文件名
        String photoFileName = image_file.getOriginalFilename();
        System.out.println("上传的文件名：" + photoFileName);
        //预防上传文件的文件名重复
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String uploadFileName = uuid + photoFileName;

        //上传文件
        File file = new File(path + photoFileName);
        //上传方法
        try {
            image_file.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        //设置属性
        user.setImgurl(photoFileName);
        userService.updateByPrimaryKeySelective(user);
        return "redirect:/user/showMyProfile";
    }

    @RequestMapping("passwordSafe")
    public ModelAndView passwordSafe(HttpSession httpSession) {
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        modelAndView.setViewName("/before/password_safe.jsp");
        return modelAndView;
    }

    @RequestMapping("updatePassword")
    public String updatePassword(String newPassword, HttpSession httpSession) {
        String userAccount = (String) httpSession.getAttribute("userAccount");
        User user = userService.selectByEmail(userAccount);
        user.setPassword(newPassword);
        userService.updateByPrimaryKeySelective(user);
        return "redirect:/user/showMyProfile";
    }

    @RequestMapping("resetPassword")
    public String resetPassword(@RequestParam("email") String email, @RequestParam("password") String password) {
        User user = userService.selectByEmail(email);
        user.setPassword(password);
        userService.updateByPrimaryKeySelective(user);
        return "redirect:/subject/selectAll";
    }

    @RequestMapping("forgetPassword")
    public String forgetPassword() {
        return "/before/forget_password.jsp";
    }


    @RequestMapping("sendEmail")
    @ResponseBody
    public String sendEmail(String email) {
        User user = userService.selectByEmail(email);
        String flag = "hasNoUser";
        String code = null;
        if (user != null) {
            code = MailUtils.getValidateCode(6);
            user.setCode(code);
            userService.updateByPrimaryKeySelective(user);
            flag = "success";
        }
        if (flag == "success") {
            MailUtils.sendMail(email, "你好，这是一封测试邮件，无需回复。", "测试邮件随机生成的验证码是：" + code);
        }
        return flag;
    }
}
